import React, {useReducer} from "react";

const reducer = (preState, action) => {
    // preState: 旧的initialState  action: dispatch函数中的参数
    console.log(preState, action);
    if (action.type === "minus") {
        const newState = {...preState}
        newState.value--
        return newState
    } else if (action.type === "plus") {
        const newState = {...preState}
        newState.value++
        return newState
    } else {
        return preState;
    }
}

const initialState = {
    value: 0,
}

export default function App() {

    const [state, dispatch] = useReducer(reducer, initialState);

    return <div>
        <button onClick={() => dispatch({type: "minus"})}>-</button>
        {state.value}
        <button onClick={() => dispatch({type: "plus"})}>+</button>
    </div>
}